package OperationSystem.day1202;

import java.util.Scanner;

public class volatileTest {
    //使用volatile修饰running保证了内存可见性，保证了其他线程修改running时，是在主内存中修改的
    //subThread这个线程就能够及时看见并作出反应
    private volatile static boolean running=true;

    private static class SubThread extends Thread{
        @Override
        public void run() {
            int n=0;
            while (running){
                n++;
            }
            System.out.println("该退出了");
        }
    }

    public static void main(String[] args) {
        SubThread subThread=new SubThread();
        subThread.start();
        Scanner scanner=new Scanner(System.in);
        scanner.nextLine();
        running=false;
    }
}
